package com.example.service.manage;

import com.example.vo.TemplateRequest;
import com.example.vo.TemplateRule;
import com.example.vo.TemplateVO;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

@Service
public class ConvertVoToRequest {
    public TemplateRequest templateVoToTemplateRequest(@NotNull TemplateVO templateVO) {
        final TemplateRequest request = new TemplateRequest();
        request.setName(templateVO.getName());
        request.setLogo(templateVO.getLogo());
        request.setDesc(templateVO.getDesc());
        request.setCategory(templateVO.getCategory());
        request.setProductLine(templateVO.getProductLine());

        request.setTarget(templateVO.getTarget());


        /*templateVO copy to TemplateRule */
        final TemplateRule rule = new TemplateRule();

        /*templateVO copy to TemplateRule.Expiration */
        final TemplateRule.Expiration expiration = new TemplateRule.Expiration();
        expiration.setPeriod(templateVO.getPeriod());
        expiration.setGap(templateVO.getGap());
        expiration.setDeadLine(templateVO.getDeadline());

        rule.setExpiration(expiration);

        /*templateVO copy to TemplateRule.Discount */
        final TemplateRule.Discount discount = new TemplateRule.Discount();
        discount.setQuota(templateVO.getQuota());
        discount.setBase(templateVO.getBase());

        rule.setDiscount(discount);
        rule.setLimitation(templateVO.getLimitation());

        /*templateVO copy to TemplateRule.Usage */
        final TemplateRule.Usage usage = new TemplateRule.Usage();
        usage.setProvince(templateVO.getProvince());
        usage.setCity(templateVO.getCity());
        usage.setGoodsType(templateVO.getGoodsType());

        rule.setUsage(usage);
        rule.setWeight(templateVO.getWeight());

        request.setRule(rule);

        return  request;
    }

    public TemplateVO  templateRequestToTemplateVO(@NotNull TemplateRequest templateRequest){
        final TemplateVO templateVO = new TemplateVO();
        templateVO.setId(1);
        templateVO.setName(templateRequest.getName());
        templateVO.setLogo(templateRequest.getLogo());
        templateVO.setDesc(templateRequest.getDesc());
        templateVO.setCategory(templateRequest.getCategory());
        templateVO.setProductLine(templateRequest.getProductLine());

        templateVO.setTarget(templateRequest.getTarget());

        /*templateRule copy to  TemplateVO*/
        final TemplateRule rule = templateRequest.getRule();

        /*templateRule.Expiration copy to  TemplateVO*/
        final TemplateRule.Expiration expiration = rule.getExpiration();
        templateVO.setPeriod(expiration.getPeriod());
        templateVO.setGap(expiration.getGap());
        templateVO.setDeadline(expiration.getDeadLine());

        /*templateRule.Discount copy to  TemplateVO*/
        final TemplateRule.Discount discount = rule.getDiscount();
        templateVO.setQuota(discount.getQuota());
        templateVO.setBase(discount.getBase());
        templateVO.setLimitation(rule.getLimitation());

        /*templateRule.Usage copy to  TemplateVO*/
        final TemplateRule.Usage usage = rule.getUsage();
        templateVO.setProvince(usage.getProvince());
        templateVO.setCity(usage.getCity());
        templateVO.setGoodsType(usage.getGoodsType());
        templateVO.setWeight(rule.getWeight());

        return templateVO;
    }
}
